<template>
  <div class="body">
    <div class="main">
      <div
        style="
          width: 300px;
          display: flex;
          flex-direction: column;
          border: 1px solid #f2f2f2;
          padding: 24px;
          background: #fff;
          border-radius: 10px;
          box-shadow: 0 0 5px #409eff;
        "
      >
        <div class="title">网站后台管理系统</div>
        <el-form ref="form" :model="form" :rules="rules" label-width="0">
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" v-model="form.username">
              <template slot="prepend"
                ><i class="el-icon-user-solid"></i
              ></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="form.password"
              show-password
            >
              <template slot="prepend"><i class="el-icon-lock"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div
              style="width: 100%; display: flex; justify-content: space-between"
            >
              <el-button
                type="primary"
                @click="handLogin"
                @keyup.enter="handLogin"
                style="width: 100%"
                >登录</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/modules/login";
import { passwordEncryption } from "@/util/encrypt";
import { getUserInfoApi } from "@/api/modules/user.api";
import { localClear, localSet } from "@/util/util";
// import { getMenuListApi } from "@/api/modules/menu.api";
// import { getModuleListApi } from "@/api/modules/modules.api";

export default {
  data() {
    return {
      logging: false,
      form: {
        username: "",
        // username: "admin",
        password: "",
        // password: "123456",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  methods: {
    handLogin() {
      localClear();
      this.$refs.form.validate((valid) => {
        if (valid) {
          const formData = new FormData();
          const password = passwordEncryption(this.form.password);
          formData.append("username", this.form.username);
          formData.append("password", password);
          login(formData)
            .then(async (item) => {
              if (item?.token) {
                localStorage.setItem("token", item.token);
                const res = await getUserInfoApi();
                // const router = await getMenuListApi();
                // const modules = await getModuleListApi();
                // const modules = await getArticleModuleListApi();
                localSet("userInfo", res);
                // localSet("router", router);
                // localSet("modules", modules);
                await this.$router.replace("/writings/list");
              }
            })
            .catch(() => {
              // this.$message.error("登陆失败");
              console.log("登陆失败");
            });
          // this.$router.replace("/writings/list");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.body {
  background: #f5f5f5;
  height: 100%;
  width: 100%;

  .main {
    height: 100%;
    width: 100%;
    background-size: cover;
    background: url("../../assets/img/bg.svg") no-repeat center center;
    display: flex;
    justify-content: center;
    align-items: center;

    .title {
      text-align: center;
      font-size: 28px;
      line-height: 100px;
    }
  }
}
</style>
